// @algorithm @lc id=100299 lang=cpp
// @title shan-chu-lian-biao-de-jie-dian-lcof

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    // @test([4,5,1,9], 5)=[4,1,9]
public:
    ListNode *deleteNode(ListNode *head, int val) {
        ListNode *temp = head;
        ListNode *prev = temp;
        while (temp != nullptr && temp->val != val) {
            prev = temp;
            temp = temp->next;
        }
        // 删除头节点
        if (temp == head) {
            return head->next;
        }
        // 没找到目标节点
        if (temp == nullptr) {
            return head;
        }
        prev->next = temp->next;
        return head;
    }
};